// mine.txt
// A magical mine which simply blows up when the
// party or any other character gets within 1 spaces of it.

// Cell 0 - Severity.
// Cell 1, 2 - SDF. Leave both at 0 for no SDF.

beginterrainscript;
variables;
    int my_cur_mess;
    int color, active = 0, working = 1;
    int red = 2000, green = 2001, blue = 2002;
body;
beginstate INIT_STATE;
    if (get_memory_cell(0) == 0)
        set_terrain_memory_cell(my_number(), 0, 5);
    set_script_mode(2);
    if ((terrain_in_this_spot() == 484) || (terrain_in_this_spot() == 487))
        color = red;
    if ((terrain_in_this_spot() == 485) || (terrain_in_this_spot() == 488))
        color = green;
    if ((terrain_in_this_spot() == 486) || (terrain_in_this_spot() == 489))
        color = blue;
    if ((get_memory_cell(1) != 0) || (get_memory_cell(2) != 0))
        if (get_flag(get_memory_cell(0), get_memory_cell(1)) != 0)
            flip_terrain(my_loc_x(), my_loc_y());
break;
beginstate START_STATE;
my_cur_mess = my_current_message();
    if ((terrain_in_this_spot() == 493) || (terrain_in_this_spot() == 494))
        working = 0;
    if (working == 0)
        end();
    if (get_nearest_char(5) != -1)
    if (dist_to_char(get_nearest_char(5)) == 1)
        active = 1;
    if (dist_to_party() == 1)
        active = 1;
    if ((active == 1) && (working == 1)) {
        if (color == red)
            set_state_continue(3);
        if (color == green)
            set_state_continue(4);
        if (color == blue)
            set_state_continue(5);
        }
break;
beginstate 3;
    put_effect_on_space(my_loc_x(), my_loc_y(), 1, 5, 1);
    if (terrain_in_this_spot() == 484) {
        put_effect_on_space(my_loc_x(), my_loc_y(), 1, 10, 1);
        put_boom_on_space(my_loc_x(), my_loc_y(), 1, 0);
        }
    print_str("You have triggered a magical mine.");
    flip_terrain(my_loc_x(), my_loc_y());
    run_animation_sound(109);
    damage_nearby(get_ran(1, 2, 16) * get_memory_cell(0), 5, 1, 2);
    if ((get_memory_cell(1) != 0) || (get_memory_cell(2) != 0))
        set_flag(get_memory_cell(1), get_memory_cell(2), 1);
    set_state(START_STATE);
break;
beginstate 4;
    put_effect_on_space(my_loc_x(), my_loc_y(), 2, 5, 1);
    if (terrain_in_this_spot() == 485) {
        put_effect_on_space(my_loc_x(), my_loc_y(), 2, 10, 1);
        put_boom_on_space(my_loc_x(), my_loc_y(), 6, 0);
        }
    print_str("You have triggered a magical mine.");
    flip_terrain(my_loc_x(), my_loc_y());
    run_animation_sound(112);
    damage_nearby(get_ran(1, 2, 16) * get_memory_cell(0), 5, 6, 2);
    if ((get_memory_cell(1) != 0) || (get_memory_cell(2) != 0))
        set_flag(get_memory_cell(1), get_memory_cell(2), 1);
    set_state(START_STATE);
break;
beginstate 5;
    put_effect_on_space(my_loc_x(), my_loc_y(), 0, 5, 1);
    if (terrain_in_this_spot() == 486) {
        put_effect_on_space(my_loc_x(), my_loc_y(), 0, 10, 1);
        put_boom_on_space(my_loc_x(), my_loc_y(), 5, 0);
        }
    print_str("You have triggered a magical mine.");
    flip_terrain(my_loc_x(), my_loc_y());
    run_animation_sound(171);
    damage_nearby(get_ran(1, 2, 16) * get_memory_cell(0), 5, 5, 2);
    if ((get_memory_cell(1) != 0) || (get_memory_cell(2) != 0))
        set_flag(get_memory_cell(1), get_memory_cell(2), 1);
    set_state(START_STATE);
break;
beginstate UNLOCK_SPELL_STATE;
   if (get_flag(111, 11) == 1)
   {
    active = 1;
    set_state_continue(START_STATE);
   }
break;
beginstate DISPEL_BARRIER_STATE;
   if (get_flag(111, 11) == 1)
   {
    put_effect_on_space(my_loc_x(), my_loc_y(), 10, 10, 0);
    run_animation_sound(163);
    flip_terrain(my_loc_x(), my_loc_y());
    working = 0;
    if ((get_memory_cell(1) != 0) || (get_memory_cell(2) != 0)) 
        set_flag(get_memory_cell(1), get_memory_cell(2), 1);
   }
break;